package persistence.DAO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

public class DAOHelper {
	

	//////////// NO SHIT
		private DataSource datasource = null;

		private static DAOHelper instance = null;

		private DAOHelper() {
			Properties propiedades = new Properties();
			try {
				propiedades.load(new FileInputStream("ConfigDB.properties"));
				
				datasource = BasicDataSourceFactory.createDataSource(propiedades);
				
				
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace(); 
			}
		}
		
		public static String md5(String clear) throws Exception {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] b = md.digest(clear.getBytes());
			int size = b.length;
			StringBuffer h = new StringBuffer(size);

			for (int i = 0; i < size; i++) {
				int u = b[i] & 255;
				if (u < 16) {
					h.append("0" + Integer.toHexString(u));
				} else {
					h.append(Integer.toHexString(u));
				}
			}
			// Clave encriptada
			return h.toString();
		}

		public static DAOHelper getInstance() {
			if (instance == null) {
				instance = new DAOHelper();
			}
			return instance;
		}

		public Connection getConnection() throws SQLException {
			return datasource.getConnection();
		}
}
